package com.qzw.demo.service.service.impl;

import com.qzw.demo.service.entity.DmOrder;
import com.qzw.demo.service.dao.OrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import com.qzw.demo.api.query.OrderQuery;
import java.lang.Integer;
import java.math.BigDecimal;
import com.qzw.common.data.BaseMysqlDao;
import com.qzw.common.service.BaseServiceImpl;
import com.qzw.demo.api.model.Order;
import org.springframework.stereotype.Service;
import java.util.Optional;
import java.lang.String;
import com.qzw.demo.api.service.IOrderService;

@Service
public class OrderServiceImpl extends BaseServiceImpl<Order, DmOrder, Integer, OrderQuery> implements IOrderService {

	@Autowired
	private OrderDao dao;

	@Override protected BaseMysqlDao getDao() {
	return dao;
	}

	@Override
	public Order entity2model(DmOrder entity) {
		Order model = null;
		if(isNotEmpty(entity)){
			model = new Order();
			model.setId(entity.getId());
			model.setOrderNo(entity.getOrderNo());
			model.setQuantity(entity.getQuantity());
			model.setAmount(entity.getAmount());
			model.setUserId(entity.getUserId());
			model.setShopId(entity.getShopId());
		}
		return model;
	}

	@Override
	public DmOrder model2Entity(Order model) {
		DmOrder entity = null;
		if(isNotEmpty(model)){
			entity = new DmOrder();
			entity.setId(model.getId());
			entity.setOrderNo(model.getOrderNo());
			entity.setQuantity(model.getQuantity());
			entity.setAmount(model.getAmount());
			entity.setUserId(model.getUserId());
			entity.setShopId(model.getShopId());
		}
		return entity;
	}

	@Override
	public Order update(Order model) {
		if(model.isNew()){
			model = super.add(model);
		} else {
			Optional<DmOrder> entityOptional = dao.findById(model.getId());DmOrder entity = entityOptional.get();
			if(isNotEmpty(model.getOrderNo()))
				entity.setOrderNo(model.getOrderNo());
			if(isNotEmpty(model.getQuantity()))
				entity.setQuantity(model.getQuantity());
			if(isNotEmpty(model.getAmount()))
				entity.setAmount(model.getAmount());
			if(isNotEmpty(model.getUserId()))
				entity.setUserId(model.getUserId());
			if(isNotEmpty(model.getShopId()))
				entity.setShopId(model.getShopId());
			model = entity2model(dao.save(entity));
		}
		return model;
	}


}